<template>
  <div class="main">
    <img src="../../../assets/push.png" width="80%" alt="请插入就诊卡">
  </div>
</template>

<script lang="ts">
import { Component } from 'vue-property-decorator'
import { mixins } from 'vue-class-component'
import BaseMixin from '@/mixins/base'
import { IBrowserResult, IPersonalInfo } from '@/interface/common'
import { GetUser } from '@/apis/api'

@Component
export default class Read extends mixins(BaseMixin) {
  private async created () {
    this.client = this.store.state.client
    this.setTip('请插入您的就诊卡')
    this.setMaxTime(60)
    this.updateTimeHandler('home')
    this.readCard(this.store.state.cardType)
  }

  private isShow (name: string) {
    return this.client.CardTypes.findIndex(e => e.Name === name) >= 0
  }

  private async readCard (cardType: '身份证' | '医保卡' | undefined) {
    this.logger.track(`读取${cardType}`)
    var result
    switch (cardType) {
      case '身份证':
        result = await this.idCardReader.readCard()
        this.process(result)
        break
      case '医保卡':
        result = await this.icCardReader.readCard()
        this.process(result)
        break
      default:
        this.showLoading('读取信息失败, 请稍后重试')
        setTimeout(() => {
          this.hideLoading()
          this.home()
        }, 3000)
        break
    }
  }

  // 读取到卡片信息, 进行业务处理
  private async process (result: IBrowserResult<IPersonalInfo>) {
    if (result && result.code === 200) {
      this.logger.track(`读取到${this.store.state.cardType}信息:${JSON.stringify(result.data)}`, this.store.state.bizType)
      if (result.data) {
        this.store.commit('updatePersonalInfo', result.data)
        // 从服务器读取建档信息
        // const response = await GetUser(result.data)
        // if (response.data.Code === 200) {
        //   this.store.commit('updatePersonalInfo', response.data.Data)
        // }
        // 根据业务类型, 跳转到不同的页面
        switch (this.store.state.bizType) {
          case '查询':
            // if (this.store.state.personalInfo.Id) {
            //   this.navigateTo(`/${this.getRoute()}/detail`)
            // } else {
            //   // 询问是否建档
            // }
            this.navigateTo(`/${this.getRoute()}/detail`)
            break
          case '建档':
            // if (this.store.state.personalInfo.Id) {
            //   this.showLoading('您已建档, 请勿重复提交. 3秒钟后返回首页')
            // } else {
            //   this.navigateTo(`/${this.getRoute()}/create`)
            // }
            this.navigateTo(`/${this.getRoute()}/create`)
            break
          case '缴费':
            this.navigateTo(`/${this.getRoute()}/create`)
            break
          default:
            this.showLoading('不支持的业务类型, 请稍后重试')
            setTimeout(() => {
              this.hideLoading()
              this.home()
            }, 3000)
            break
        }
      } else {
        const handler = setTimeout(() => {
          this.readCard(this.store.state.cardType)
        }, 1000)
        this.handlers.push(handler)
      }
    } else {
      this.logger.warning(`读取${this.store.state.cardType}信息失败, ${result.message}`)
      this.showLoading('读取信息失败, 请稍后重试')
      setTimeout(() => {
        this.hideLoading()
        this.home()
      }, 3000)
    }
  }
}
</script>

<style scoped>
  .main{
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    align-content: center;
  }
</style>
